﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using blackhouse.WebNemo.Data.Cache;
using System.Data.SqlClient;
using blackhouse.WebNemo.Data;

namespace blackhouse.WebNemo.Business
{
    public class SqlFactory : Factory
    {

        private static string usingVersion;

        public override IDataAccess GetDataAccess() {
            blackhouse.WebNemo.Data.IDataAccess da = null;
            blackhouse.WebNemo.Data.ISupportSystemNames ssn = null;

            switch (GetUsingVersion()) {
                case "Beta":
                    da = new blackhouse.WebNemo.Data.SqlDataAccess(GetConnectionString());
                    break;
                case "Stronger":
                    da = new blackhouse.WebNemo.Data.Stronger.SqlDataAccess(GetConnectionString());
                    break;
                default:
                    throw new Exception("Brak zainstalowanej wersji bazowej programu NEMO");
            }
            ssn = da as blackhouse.WebNemo.Data.ISupportSystemNames;

            if (ConfigurationManager.AppSettings["PreventNemoCache"] != "true") {
                var cacheTime = 20;
                var sct = ConfigurationManager.AppSettings["CacheTime"];
                if (!String.IsNullOrEmpty(sct)) {
                    int sctt;
                    if (Int32.TryParse(sct, out sctt))
                        cacheTime = sctt;
                }
                da = new CacheDataAccess(da, TimeSpan.FromMinutes(cacheTime), GetConnectionString());
            }

            if (ssn != null) ssn.Names = new Data.SystemNames(da);

            return da;
        }

        private static string connectionString = String.Empty;

        protected virtual string GetConnectionString() {
            if (connectionString == String.Empty) {
                string connString = ConfigurationManager.AppSettings["MSSQLConnectionString"];
                if (string.IsNullOrEmpty(connString)) {
                    ConnectionStringSettings css = ConfigurationManager.ConnectionStrings["MSSQLConnectionString"];
                    if (css == null && ConfigurationManager.ConnectionStrings.Count > 0)
                        css = ConfigurationManager.ConnectionStrings[0];
                    if (css != null)
                        connString = css.ConnectionString;
                }
                connectionString = connString;
            }
            return connectionString;
        }

        public string GetUsingVersion() {
            if (!String.IsNullOrEmpty(usingVersion)) return usingVersion;
            using (var connection = new SqlConnection(GetConnectionString())) {
                connection.Open();
                var cmd = connection.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = @"SELECT CASE
					WHEN OBJECT_ID('n_Tree') IS NOT NULL THEN 'Stronger'
					WHEN OBJECT_ID('sTree') IS NOT NULL THEN 'Beta'
				END";
                usingVersion = Convert.ToString(cmd.ExecuteScalar());
                connection.Close();
            }
            return usingVersion;
        }

    }
}
